<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="黑匣子唯一标识:">
              <a-input style="width: 200px" v-model:value.trim="where.vehicleImei" placeholder="请输入黑匣子唯一标识" allow-clear />
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="vehicleId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >
          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openClientListDialog(record)">多边形围栏</a>
                <a-divider type="vertical" />
                <a @click="openClientCircleDialog(record)">圆形围栏</a>
                <a-divider type="vertical" />
                <a @click="openPolygonListDialog(record)">车辆围栏(服务端计算)</a>
              </a-space>
            </template>

            <template v-if="column.key === 'runningState'">
              <span v-if="!record.deviceRecord || record.deviceRecord.recordStatus == 2">离线</span>
              <span v-else-if="record.vehicleRealtimeData && record.vehicleRealtimeData.state == 'running'">行驶中</span>
              <span v-else-if="record.vehicleRealtimeData && record.vehicleRealtimeData.state == 'idling'">怠速</span>
              <span v-else-if="record.vehicleRealtimeData && record.vehicleRealtimeData.state == 'ignition_off'">熄火</span>
              <span v-else>未知</span>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <vehicle-manager-geofence-list-polygon-client v-if="showListClient" v-model:clientVisible="showListClient" :data="current" @done="reload" />
    <vehicle-manager-geofence-list-circle-client v-if="showCircleClient" v-model:circleVisible="showCircleClient" :data="current" @done="reload" />
    <VehicleManagerGeofenceVehicleManagerFencelist v-if="showPolygonList" v-model:listVisible="showPolygonList" :data="current" @done="reload" />
  </div>
</template>

<script>
import { VehicleApi } from '@/api/property/vechile/VehicleApi';
import { message } from 'ant-design-vue';
import VehicleManagerGeofenceListPolygonClient from './fencelist-polygon-client.vue'
import VehicleManagerGeofenceListCircleClient from './fencelist-circle-client.vue'
import VehicleManagerGeofenceVehicleManagerFencelist from './vehiclefence/vehiclefence-fencelist.vue'

export default {
  name: 'VehicleManagerGeofence',
  components: {
    VehicleManagerGeofenceListPolygonClient,
    VehicleManagerGeofenceListCircleClient,
    VehicleManagerGeofenceVehicleManagerFencelist
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '车辆id',
          dataIndex: 'vehicleId'
        },
        {
          title: '车辆名字',
          dataIndex: 'vehicleName'
        },
        {
          title: '车牌号',
          dataIndex: 'license'
        },
        {
          title: '运行状态',
          dataIndex: 'vehicleRealtimeData',
          key : 'runningState',
          width: 100,
        },
        {
          title: '行驶速度(km/h)',
          dataIndex: 'vehicleRealtimeData',
          customRender: function(row) {
            console.log(row)
            if (row.record.vehicleRealtimeData != null) {
              return  row.record.vehicleRealtimeData.speed ? row.record.vehicleRealtimeData.speed : 0;
            } else {
              return 0;
            }
          }
        },
        {
          title: '操作',
          key: 'action',
          width: 350,
          align: 'center',
          fixed: 'right'
        }
      ],
      // 表格搜索条件
      where: {},
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      loadTimer:null, //定义一个刷新的timer
      showListClient : false,
      showCircleClient : false,
      showPolygonList : false,
    };
  },
  mounted() {
    this.loadTimer = setInterval(this.loadPage, 30000);
  },
  beforeUnmount() {
    clearInterval(this.loadTimer);
  },
  methods: {
    loadPage() {
      this.$refs.table.reload({ });
    },
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2023/10/03 23:15
     */
    reload() {
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2023/10/03 23:15
     */
    reset() {
      this.where.vehicleImei = '';
      this.$nextTick(() => {
        this.reload();
      });
    },

    openClientListDialog(row) {
      this.current = row;
      this.showListClient = true;
    },

    openClientCircleDialog(row) {
      this.current = row;
      this.showCircleClient = true;
    },
    openPolygonListDialog(row) {
      this.current = row;
      this.showPolygonList = true;
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2023/10/03 23:15
     */
    async remove(row) {
      const result = await VehicleApi.delete({ vehicleId: row.vehicleId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return VehicleApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
